package com.david.server.model;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.david.client.dto.UserDTO;
import com.david.client.rpc.ILoginService;

public class User {
	private UserDTO userDTO = null;

	public User(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public Integer login(Session hbsession) {
		hbsession.beginTransaction();
		String hql = "from UserDTO where loginName = '" + userDTO.getLoginName() + "'";
		Query query = hbsession.createQuery(hql);
		List list = query.list();
		hbsession.getTransaction().commit();
		if (list == null)
			return ILoginService.ERROR;

		if (list.size() != 1) {
			return ILoginService.NAMENOTEXISTS;
		}

		UserDTO tmpUser = (UserDTO) list.get(0);
		if (tmpUser.getPasswd().equals(userDTO.getPasswd())) {
			userDTO.setUserID(tmpUser.getUserID());
			return ILoginService.SUCCESS;
		} else {
			return ILoginService.PASSWDINVAID;
		}
	}
}
